package com.hsck.order.kds.setup.adapter

import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import com.hsck.order.kds.R
import com.hsck.order.kds.base.bean.CategoryBean
import com.hsck.order.kds.base.common.BaseAdapter
import com.hsck.order.kds.base.common.BaseViewHolder
import com.hsck.order.kds.base.common.setOnSingleClickListener
import com.hsck.order.kds.base.common.setSpannableString
import com.hsck.order.kds.base.utils.LogUtil
import kotlinx.android.synthetic.main.item_food_classif.view.*

/**
 * 沽清菜品分类-适配器
 */
class FoodClassifAdapter(@LayoutRes layoutId: Int) :
    BaseAdapter<CategoryBean>(layoutId) {

    var selectPosition = 0
    var recyclerView: RecyclerView? = null

    var onPositionClick: ((CategoryBean, Int) -> Unit) = { showBean, index -> }


    override fun onBindViewHolder(holder: BaseViewHolder, data: CategoryBean) {
        holder.itemView.tv_item_food_classif_name.text = "全部${data.tagName}"


        if (data.isSelected) {
            holder.itemView.setBackgroundResource(R.drawable.shape_redbd_borderless10)
            holder.itemView.tv_item_food_classif_name.setSpannableString(otherTextSize = 40)
        } else {
            holder.itemView.setBackgroundResource(R.drawable.shape_gray9d_borderless10)
            holder.itemView.tv_item_food_classif_name.setSpannableString(otherTextSize = 36)

        }
        holder.itemView.setOnSingleClickListener {
            try {
                onPositionClick.invoke(data, holder.adapterPosition)
            } catch (e: Exception) {
                LogUtil.e("error" + e.message)
            }
        }
    }

    private fun setSingleChoiceOnClick(nowholder: BaseViewHolder, position: Int) {

        if (selectPosition != -1) {
            //实现单选方法三： RecyclerView另一种定向刷新方法：不会有白光一闪动画 也不会重复onBindVIewHolder
            val holder = recyclerView!!.findViewHolderForLayoutPosition(selectPosition)
            if (holder != null) {
                notifyItemChanged(selectPosition)
            } else {
                //add by 2016 11 22 for 一些极端情况，holder被缓存在Recycler的cacheView里，
                //此时拿不到ViewHolder，但是也不会回调onBindViewHolder方法。所以add一个异常处理
                notifyItemChanged(selectPosition)
            }
            //不管在不在屏幕里 都需要改变数据
            datas[selectPosition].isSelected = false
            notifyItemChanged(selectPosition)
            //设置新Item的勾选状态
            selectPosition = position
            datas[selectPosition].isSelected = true

        } else {
            //设置新Item的勾选状态
            selectPosition = position
            datas[selectPosition].isSelected = true
        }
        notifyItemChanged(selectPosition)
    }

}